package com.tanhua.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/movements")
public class MovementsController {
    //圈子动态的Controller

    //注入对应的service
    @Autowired
    private MovementsService movementsService;

    /*
        发布朋友圈
        Post请求
        参数：textContent 文字动态
              imageContent 图片动态
              location     位置
              longitude    精度
              latitude     维度
        返回值：void
     */
    @PostMapping//发朋友圈
    public ResponseEntity movements(String textContent, //文字动态
                                    MultipartFile imageContent[], //图片动态,这里使用MultipartFile来接收
                                    String location,    //位置动态
                                    String longitude,   //经纬动态
                                    String latitude) throws IOException {
        //1.接收参数

        //2.调用service处理
        movementsService.movements(textContent, imageContent, location, longitude, latitude);
        //3.返回
        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")//查询自己的动态
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId) {
        //1.接收参数
        //2.调用service处理数据
        PageResult vo = movementsService.all(page, pagesize, userId);
        //3.返回参数（这里返回的结果要求是一个分页对象）
        return ResponseEntity.ok(vo);
    }

    @GetMapping//查询好友动态
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1.接收参数,前端传递了两个分页参数

        //2.调用service查询,前端要求返回一个分页对象
        PageResult vo = movementsService.findByFriendId(page, pagesize);
        //3.返回参数，返回的这个vo参数当中有分页信息,用户信息还有动态信息
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/recommend")//查询推荐动态
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        //1.接收前端传递过来的参数
        //2.调用service处理,这里的返回结果包括,分页信息,用户信息,动态信息
        PageResult vo = movementsService.findByRecommend(page, pagesize);
        //3.给前端返回参数
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{id}")    //根据id查询动态
    public ResponseEntity findById(@PathVariable("id") String id) {
        //1.接收参数

        //2.调用service处理
        MovementsVo vo = movementsService.findById(id);
        //3.返回结果
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/visitors")//防止报错的id
    public ResponseEntity visitors(){
        return ResponseEntity.ok(null);
    }


    @GetMapping("/{id}/like")//点赞
    public ResponseEntity like(@PathVariable("id")String movementId){
        //1.获取参数
        //2.调用service处理
       Integer count= movementsService.like(movementId);
        //3.返回数据结果
        return ResponseEntity.ok(count);
    }


    @GetMapping("/{id}/love")//喜欢
    public ResponseEntity love(@PathVariable("id")String movementId){
        //1.获取参数
        //2.调用service处理
        Integer count= movementsService.love(movementId);
        //3.返回数据结果
        return ResponseEntity.ok(count);
    }


    @GetMapping("/{id}/dislike")//取消点赞
    public ResponseEntity dislike(@PathVariable("id")String movementId){
        //1.获取参数
        //2.调用service处理
        Integer count= movementsService.dislike(movementId);
        //3.返回数据结果
        return ResponseEntity.ok(count);
    }


    @GetMapping("/{id}/unlove")//取消喜欢
    public ResponseEntity unlove(@PathVariable("id")String movementId){
        //1.获取参数
        //2.调用service处理
        Integer count= movementsService.unlove(movementId);
        //3.返回数据结果
        return ResponseEntity.ok(count);
    }
}
